package com.tpp.threat_perception_platform.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tpp.threat_perception_platform.dao.HostVulMapper;
import com.tpp.threat_perception_platform.param.MyParam;
import com.tpp.threat_perception_platform.pojo.HostVul;

import com.tpp.threat_perception_platform.response.ResponseResult;
import com.tpp.threat_perception_platform.service.HostVulService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
public class HostVulServiceImpl implements HostVulService {
    @Autowired
    private HostVulMapper hostVulMapper;
    @Override
    public int saveHostVul(HostVul hostVul) {
        // 设置公共属性
        hostVul.setDetectTime(new Timestamp(System.currentTimeMillis()));
        // 插入新记录
        return hostVulMapper.insertSelective(hostVul);
    }

    @Override
    public void deleteByMacAddress(String macAddress) {
        hostVulMapper.deleteByMacAddress(macAddress);
    }

    @Override
    public ResponseResult hostVulList(MyParam param) {
        // 设置分页参数
        PageHelper.startPage(param.getPage(), param.getLimit());

        List<HostVul> dataList;

        // 判断是否有具体查询条件
        if (param.getMacAddress() != null && !param.getMacAddress().isEmpty()) {
            // 按MAC地址查询
            dataList = hostVulMapper.findByMacAddress(param);
        } else if (param.getHostName() != null && !param.getHostName().isEmpty()) {
            // 按主机名查询
            dataList = hostVulMapper.findByHostName(param);
        } else if (param.getVulName() != null && !param.getVulName().isEmpty()) {
            // 按漏洞名查询
            dataList = hostVulMapper.findByVulName(param);
        } else if (param.getVulLevel() != null && !param.getVulLevel().isEmpty()) {
            // 按漏洞等级查询
            dataList = hostVulMapper.findByVulLevel(param);
        } else {
            // 无具体条件时查询所有
            dataList = hostVulMapper.findAll(param);
        }

        // 构架pageInfo
        PageInfo<HostVul> pageInfo = new PageInfo<>(dataList);
        return new ResponseResult<>(pageInfo.getTotal(), pageInfo.getList());
    }
}
